'''
$Revision: 17 $
$LastChangedDate: 2007-05-11 16:00:08 +0000 (Fri, 11 May 2007) $
Some basic unittesting
Really need to expand this.
'''
import unittest

import os, sys
from sandbox.InfixParser import MyInfixParser

class TestInfixParser(unittest.TestCase):
    '''Tests the infix parser'''
    
    def setUp(self):
        self.m = MyInfixParser()
        self.m.buildlexer()
        self.m.buildparser(debug=0, debugfile='infix.dbg', tabmodule='infix_tabmodule')
        self.m.setNameStr('self.', '()')


    def testSingleNumber(self):
        expr = "0"
        self.m.parse(expr)
        self.assertEqual(self.m.input, expr)
        self.assertEqual(self.m.output,"0")
        self.m.names.sort()
        self.assertEqual(self.m.names,[])   

    def testVariableNames(self):
        expr = "K4K4*K4"
        self.m.parse(expr)
        self.assertEqual(self.m.input, expr)
        self.assertEqual(self.m.output,"self.K4K4()*self.K4()")
        self.m.names.sort()
        self.assertEqual(self.m.names,['K4','K4K4'])   

    def testSimplePowers(self):
        expr = "(K3*K4+s0)**(h)"
        self.m.parse(expr)
        self.assertEqual(self.m.input, expr)
        self.assertEqual(self.m.output,"(self.K3()*self.K4()+self.s0())**(self.h())")
        self.m.names.sort()
        self.assertEqual(self.m.names,['K3','K4', 'h', 's0'])

        expr = "(K3*K4+s0)**h"
        self.m.parse(expr)
        self.assertEqual(self.m.input, expr)
        self.assertEqual(self.m.output,"(self.K3()*self.K4()+self.s0())**self.h()")
        self.m.names.sort()
        self.assertEqual(self.m.names,['K3','K4', 'h', 's0'])


        expr = "(2**K3*K4+s0)**(h/5)"
        self.m.parse(expr)
        self.assertEqual(self.m.input, expr)
        self.assertEqual(self.m.output,"(2**self.K3()*self.K4()+self.s0())**(self.h()/5)")
        self.m.names.sort()
        self.assertEqual(self.m.names,['K3','K4', 'h', 's0'])

        
if __name__ == '__main__':
    unittest.main()
